/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Ap0stoL2
 * Date: 2023-07-26
 * Time: 8:53
 */
//测试wait 和 notify
public class Demo15 {

    //使用这个锁对象来负责作为wait和notify的加锁解锁对象
    private static  Object locker = new Object();
    public static void main(String[] args) {
        //线程1
        Thread t1 = new Thread(()->{
           while(true) {
               //内部类调用外部类
               synchronized (locker) {
                   System.out.println("t1 开始 进行 wait");
                   try {
                       locker.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println("t1 线程wait结束");//wait会让t1卡在wait那一步,直到notify才会执行这个打印语句
               }

           }
        });
        t1.start();

        //线程2
        Thread t2 = new Thread(()->{
            //让notify发生的时间晚于wait()
           while(true){
               try {
                   Thread.sleep(5000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               synchronized (locker){
                   System.out.println("t2 notify 开始");
                   locker.notify();
                   System.out.println("t2 notify 结束");
                   System.out.println("------------------------------------");
               }
           }
        });
        t2.start();
    }
}
